package com.lsk.mps.backend.redis;

import com.lsk.mps.backend.util.SseEmittersHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import redis.clients.jedis.JedisPubSub;

import java.io.IOException;

@Slf4j
public class Subscriber extends JedisPubSub {
    @Override
    public void onMessage(String channel, String message) {
        SseEmittersHolder.emitters().forEach(entry -> {
            log.info("Send message " + message + " to client " + entry.getKey());
            try {
                entry.getValue().send(message, MediaType.TEXT_PLAIN);
            } catch (IOException e) {
                log.error("Cannot send message to client " + entry.getKey(), e);
            }
        });
    }
}
